﻿namespace com.vs.General
{
    using Microsoft.Win32;
    using System;
    using System.Collections.Specialized;
    using System.Configuration;
    using System.Diagnostics;
    using System.Xml;

    public class ConfigManager
    {
        private static string _file;
        private static XmlDocument _wmfwConfig;

        static ConfigManager()
        {
            RegistryKey key = null;
            try
            {
                using (key = Registry.LocalMachine.OpenSubKey(Constants._REGISTRYPATH))
                {
                    if ((key != null) && KeyValuePresent(key, Constants._CONFIGPATH))
                    {
                        _file = key.GetValue(Constants._CONFIGPATH).ToString();
                        _wmfwConfig = new XmlDocument();
                        _wmfwConfig.Load(_file);
                    }
                    else
                    {
                        EventLog.WriteEntry("ConfigManager", "Registry Key " + Constants._REGISTRYPATH + " or Registry Key value " + Constants._CONFIGPATH + " not found", EventLogEntryType.Error);
                    }
                }
            }
            catch (Exception exception)
            {
                throw new Exception("Unable to load config file: " + _file, exception);
            }
        }

        public static void CommitChanges(Session session)
        {
            _wmfwConfig.Save(_file);
        }

        public static object GetConfig(string xPath)
        {
            XmlAttribute attribute = null;
            if (_wmfwConfig == null)
            {
                return "";
            }
            XmlNode node = _wmfwConfig.SelectSingleNode("XWMS/" + xPath);
            if (node == null)
            {
                return null;
            }
            if ((node.Attributes != null) && ((attribute = (XmlAttribute) node.Attributes.GetNamedItem("type")) != null))
            {
                return XMLDocManager.Deserialize(attribute.Value, node.InnerXml);
            }
            return node.InnerText;
        }

        public static string GetConfig(string xPath, string key)
        {
            NameValueCollection section = (NameValueCollection) ConfigurationManager.GetSection(xPath);
            if (section == null)
            {
                return null;
            }
            return section[key];
        }

        public static decimal GetElementDecimalValue(Session session, string xPath)
        {
            decimal num;
            if (decimal.TryParse(GetElementStringValue(session, xPath), out num))
            {
                return num;
            }
            return 0M;
        }

        public static double GetElementDoubleValue(Session session, string xPath)
        {
            double num;
            if (double.TryParse(GetElementStringValue(session, xPath), out num))
            {
                return num;
            }
            return 0.0;
        }

        public static int GetElementIntValue(Session session, string xPath)
        {
            int num;
            if (int.TryParse(GetElementStringValue(session, xPath), out num))
            {
                return num;
            }
            return 0;
        }

        public static string GetElementStringValue(Session session, string xPath)
        {
            XmlNode node = _wmfwConfig.SelectSingleNode("WMFW/" + xPath);
            if (node == null)
            {
                return null;
            }
            return node.InnerText;
        }

        private static bool KeyValuePresent(RegistryKey key, string keyValue)
        {
            string[] valueNames = null;
            if (key.ValueCount != 0)
            {
                valueNames = key.GetValueNames();
                for (int i = 0; i < valueNames.Length; i++)
                {
                    if (valueNames[i] == keyValue)
                    {
                        return true;
                    }
                }
                return false;
            }
            return false;
        }

        public static void RemoveNode(Session session, string xPath)
        {
            XmlNode oldChild = _wmfwConfig.SelectSingleNode("WMFW/" + xPath);
            if (oldChild != null)
            {
                oldChild.ParentNode.RemoveChild(oldChild);
            }
        }

        public static void SetElementValue(Session session, string xPath, decimal value)
        {
            SetElementValue(session, xPath, value);
        }

        public static void SetElementValue(Session session, string xPath, double value)
        {
            SetElementValue(session, xPath, value);
        }

        public static void SetElementValue(Session session, string xPath, int value)
        {
            SetElementValue(session, xPath, value.ToString());
        }

        public static void SetElementValue(Session session, string xPath, string value)
        {
            XmlNode newChild = _wmfwConfig.SelectSingleNode("WMFW/" + xPath);
            if (newChild == null)
            {
                string[] strArray = xPath.Split(new char[] { '/' });
                XmlNode node2 = _wmfwConfig.SelectSingleNode("WMFW");
                for (int i = 0; i < strArray.Length; i++)
                {
                    newChild = node2.SelectSingleNode(strArray[i]);
                    if (newChild == null)
                    {
                        newChild = _wmfwConfig.CreateElement(strArray[i]);
                        node2.AppendChild(newChild);
                    }
                    node2 = newChild;
                }
            }
            newChild.InnerText = value;
        }

        public static string ConfigFile
        {
            get
            {
                return _file.Replace("file:///", "").Replace("/", @"\");
            }
        }
    }
}

